package com.scada.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.model.bean.Role;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.service.RoleService;


/**
 * @author 黄胜
 * 16-10-26 11:24
 * 系统角色方法
 *  
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController{
	
	@Autowired
	private RoleService roleService;
	
	/** 
	 * @FunctionName: toMain
	 * @Description: 跳转到角色管理页面
	 * @author: 沈浩
	 * @date: 2016年11月3日 上午10:33:35 
	 * @return 
	 */
	@RequestMapping(value = "toRole")
	public ModelAndView toMain(){
		
		
		return new ModelAndView("system/role");
	}
	
	@RequestMapping("selectAllDataList")
	@ResponseBody
	public PageInfo<Role> selectAllDataList(){
		ResponseBean rb = roleService.selectAllDataList();
		PageInfo<Role> p = new PageInfo<Role>();
		if(StringUtils.equals(ResponseBean.SUCCESS, rb.getType())){
			p.setData((List<Role>)rb.getObject("roleList"));
		}
		return p;
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public ModelAndView insert(Role role){
		ResponseBean rb = new ResponseBean();
		if(StringUtils.isNotBlank(role.getSysRoleCode())){
			role.setCreateUserId(super.getCurrentUser().getId());
			ResponseBean insertRb = roleService.insert(role);
			if(StringUtils.equals(ResponseBean.SUCCESS, insertRb.getType())){
				rb.success(insertRb.getMsg());
			}else{
				rb.failed(insertRb.getMsg());
			}
		}else{
			rb.failed("编码值不能为空");
		}
		return rb.getJsonModelView();
	}
	
	@RequestMapping("update")
	@ResponseBody
	public ModelAndView update(Role role){
		ResponseBean rb = new ResponseBean();
		if(role.getId() != null && role.getId() != 0){
			ResponseBean updateRb = roleService.updateByPrimaryKey(role);
			if(StringUtils.equals(ResponseBean.SUCCESS, updateRb.getType())){
				rb.success(updateRb.getMsg());
			}else{
				rb.failed(updateRb.getMsg());
			}
		}else{
			rb.failed("记录ID不能为空");
		}
		return rb.getJsonModelView();
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public ModelAndView delete(String ids){
		ResponseBean rb = new ResponseBean();
		if(StringUtils.isNotBlank(ids)){
			ResponseBean deleteRb = roleService.deleteByPrimaryKey(ids);
			if(StringUtils.equals(ResponseBean.SUCCESS, deleteRb.getType())){
				rb.success(deleteRb.getMsg());
			}else{
				rb.failed(deleteRb.getMsg());
			}
		}else{
			rb.failed("需要删除的记录ID不能为空");
		}
		return rb.getJsonModelView();
	}
	
	@RequestMapping("findById")
	@ResponseBody
	public Role findById(Integer id){
		ResponseBean rb = roleService.selectRoleByPrimaryKey(id);
		if(StringUtils.equals(ResponseBean.SUCCESS, rb.getType())){
			return (Role)rb.getObject("role");
		}
		return null;
	}
}
